package com.zjs.vueUser.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjs.vueUser.entity.User;
import com.zjs.vueUser.entity.UserQuery;
import com.zjs.vueUser.entity.UserRole;
import com.zjs.vueUser.mapper.UserMapper;
import com.zjs.vueUser.service.UserService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zjs
 * @since 2020-07-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

//    @Autowired
//    private UserMapper userMapper;
/*    @Autowired
    private RedisTemplate<String,Object> redisTemplate;*/

    @Override
    public User userLogin(User user) {
//        redisTemplate.opsForValue().set(user.getUsername(),user);
//        User o = (User)redisTemplate.opsForValue().get(user.getUsername());


        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword());
        return getOne(wrapper);
    }

    @Override
    public IPage<User> userLlst(UserQuery query) {
        return  this.page(new Page<>(query.getCurrent(),query.getSize()),
                new QueryWrapper<User>().lambda().like(StrUtil.isNotBlank(query.getUsername()),User::getUsername,query.getUsername()));
    }

    @Override
    public UserRole getRoleId(Long id) {
        return baseMapper.getUserRole(id);
    }

    @Override
    public void saveOrUpdateRoleUser(UserRole role) {
        if(role.getId() != null){
            baseMapper.updateRoleUser(role);
        }else{
            baseMapper.saveRoleUser(role);
        }
    }
}
